using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace PHSRAG.Insight21.InsightWebControls
{
	#region Class SubNavigation
	/// <summary>
	/// The SubNavigation class is the base for all other SubNavigation objects.
	/// </summary>
	[ToolboxData("<{0}:SubNavigation runat=server></{0}:SubNavigation>")]
	public abstract class SubNavigation : System.Web.UI.WebControls.WebControl, INamingContainer
	{
		#region Constants
		private const string NavigateCommand = "Navigate";
		#endregion

		#region Events
		/// <summary>
		/// Event to be handled by containing page.
		/// </summary>
		public event CommandEventHandler Navigate 
		{
			add		{ Events.AddHandler(NavigateCommand, value); }
			remove	{ Events.RemoveHandler(NavigateCommand, value); }
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Create all controls that will be on the SubNavigation. Notice that the creation is more than just instantiating
		/// the controls. It also involves in generating the underlying HTML so that the controls shows up per the need.
		/// </summary>
		protected override void CreateChildControls()
		{
			CreateControls();
			GenerateControlTree();
		}

		/// <summary>
		/// Events raised only by link buttons are handled. The name of the button which raised the event is stored
		/// in the session and the associated URL is navigated to. The name stored in the session is used during page
		/// load to highlight the link which was last clicked.
		/// </summary>
		/// <param name="source">Reference to the child control that raised the event</param>
		/// <param name="e">Argument associated with the event</param>
		/// <returns>True if the event was handle by this method; false otherwise</returns>
		protected override bool OnBubbleEvent(object source, EventArgs e)
		{
			bool handled = false;
            LinkButton button = source as LinkButton;
			if ((e is CommandEventArgs) && (button != null))
			{
				ManageSelection(button.CommandName);

				CommandEventHandler handler = (CommandEventHandler)Events[NavigateCommand];
				try
				{
					// if the page doesn't want to redirect, it can throw an exception to prevent redirection
					if (handler != null)
						handler(this, new CommandEventArgs(button.CommandName, button.CommandArgument));
					Page.Response.Redirect(button.CommandArgument,false);
				}
				catch{}

				handled = true;
			}
			return handled;
		}

		/// <summary>
		/// Ensure that all child controls are created.
		/// </summary>
		/// <param name="e">Module(e.g Agreement) associated with the event</param>
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			EnsureChildControls();
		}
		#endregion
		
		#region Internal Methods
		/// <summary>
		/// This method is ONLY invoked by the associated designer 
		/// to show the control contents at design time.
		/// </summary>
		internal void Design()
		{
			EnsureChildControls();
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Removes the selected link from session cache.
		/// </summary>
		public void Reset()
		{
			Page.Session.Remove(SelectedSubNavigationLinkKey);
		}
		#endregion

		#region abstract Properties and Methods to be implemented by derived classes
		/// <summary>
		/// Get the selected link name
		/// </summary>
		protected abstract string SelectedSubNavigationLinkKey { get; }		
		
		/// <summary>
		/// Create all child controls.
		/// </summary>
		protected abstract void CreateControls();

		/// <summary>
		/// Generate the tree of child controls.
		/// </summary>
		protected abstract void GenerateControlTree();

		/// <summary>
		/// Get the link button which has been currently selected.
		/// </summary>
		/// <returns>Selected LinkButton</returns>
		protected abstract LinkButton GetSelectedLink();
		#endregion

		#region Virtual Methods
		/// <summary>
		/// 
		/// </summary>
		/// <param name="commandName"></param>
		protected virtual void ManageSelection(string commandName)
		{
			Page.Session[SelectedSubNavigationLinkKey] = commandName;
		}
		#endregion
	}
	#endregion
}

// --- EOF ----
